package com.bj58.groupbuy.action.inter;

import java.util.Date;
import java.util.List;

import com.bj58.groupbuy.action.db.CommonDao;
import com.bj58.groupbuy.action.db.DBConfig;
import com.bj58.groupbuy.action.model.CPCRealTimeWatchVo;
import com.bj58.groupbuy.action.util.DateUtil;
import com.bj58.groupbuy.action.util.SomePublicUtil;

/**CPC实时数据 - 小时级监控 （对比燕杰的数据）
 * @author HL
 *
 */
public class CPCRealTimeWatch {

	private CommonDao comDao = new CommonDao();
	private SomePublicUtil spu = new SomePublicUtil();
	double warnNum = 0.05; //预警误差阀值 
	
	/**获取燕杰的CPC小时汇总数据
	 */
	@SuppressWarnings("static-access")
	private  CPCRealTimeWatchVo  getDataFromYJ(String date,int hour){
		try {
			String month = date.substring(0,date.lastIndexOf("-")).replace("-", "");
			StringBuffer sql = new StringBuffer();
			sql.append(" SELECT sum(cash)/100,sum(cost)/100,sum(click) ");
			sql.append(" FROM t_jingzhun_user_click_aduserantispam_hour_" + month );
			sql.append(" where stat_date = '"+date+"' ");
			sql.append(" and stat_hour =  " + hour );
			
			List<CPCRealTimeWatchVo> rsList = comDao.getAllColsByVo(sql.toString(), CPCRealTimeWatchVo.class, DBConfig.db_131_data_bi.getIntType());
			return rsList.get(0);
			
		} catch (Exception e) {
			e.printStackTrace();
			spu.emailWarnHL("CPC实时数据：获取燕杰的"+hour+"点小时数据失败！");
		}
		return null;
	}
	
	
	/**获取实时数据的CPC小时汇总数据
	 */
	@SuppressWarnings("static-access")
	private  CPCRealTimeWatchVo  getDataFromRealTime(String date,int hour){
		try {
			StringBuffer sql = new StringBuffer();
			
			sql.append(" SELECT sum(cash)/100,sum(cost)/100,sum(pv) ");
			sql.append(" FROM click_"+date.replace("-","")  );
			sql.append(" WHERE FROM_UNIXTIME(time/1000, '%Y%m%d%k' ) = '"+date.replace("-","") + hour + "' ");
			
			List<CPCRealTimeWatchVo> rsList = comDao.getAllColsByVo(sql.toString(), CPCRealTimeWatchVo.class, DBConfig.db_realtime.getIntType());
			return rsList.get(0);
			
		} catch (Exception e) {
			e.printStackTrace();
			spu.emailWarnHL("CPC实时数据：获取"+hour+"点小时数据失败！");
		}
		return null;
	}
	
	
	/**数据对比是否超过阀值
	 */
	private boolean checkRate (long num1 , long num2){
		double rate =   (double)  (num1 - num2)/num1 ;
		if(Math.abs(rate) > warnNum){
			System.out.println(rate);
			return false;
		}else{
			return true;
		}
	
	}
	
	/**数据对比
	 */
	@SuppressWarnings("static-access")
	private boolean compareData(CPCRealTimeWatchVo  yj, CPCRealTimeWatchVo rt){
	
		try {
			//比较cash
			long cash1 = yj.getCash();
			long cash2 = rt.getCash();
			
			String data = "cash-yj:" + cash1 +"; cash-rt : " + cash2;
			
			if(!checkRate(cash1, cash2)){
				spu.emailWarnHL("CPC实时数据: 小时数据对比 - cash预警！当前预警阀值：" + warnNum * 100 + "% ; " + data);
				return false;
			}
			
			//比较cost
			long cost1 = yj.getCost();
			long cost2 = rt.getCost();
			
			data = "cost-yj:" + cost1 +"; cost-rt : " + cost2;
			
			if(!checkRate(cost1, cost2)){
				spu.emailWarnHL("CPC实时数据: 小时数据对比 - cost预警！当前预警阀值：" + warnNum * 100 + "%" + data);
				return false;
			}
			
			//比较click
			long click1 = yj.getClick();
			long click2 = rt.getClick();
			
			data = "click-yj:" + click1 +"; click-rt : " + click2;
			
			if(!checkRate(click1, click2)){
				spu.emailWarnHL("CPC实时数据: 小时数据对比 - click预警！当前预警阀值：" + warnNum * 100 + "%"+ data);
				return false;
			}
		} catch (Exception e) {
			e.printStackTrace();
			spu.emailWarnHL("CPC实时数据:数据对比出错！");
			return false;
		}
		return true;
	}
	
	
	@SuppressWarnings({ "static-access", "deprecation" })
	public static void main(String[] args) {
		
		SomePublicUtil spu = new SomePublicUtil();
		CPCRealTimeWatch  watch = new CPCRealTimeWatch();
		try {
			String date = DateUtil.getStringDate(new Date(),"yyyy-MM-dd");
			int hour =  new Date().getHours() -1 ;  //每次检查上1小时段的数据
			
			if(hour == -1 ){  // 每晚0点时检查上一天23点数据
				date = DateUtil.getStringDate(DateUtil.getDateBefore(new Date(), 1), "yyyy-MM-dd");
				hour = 23;
			}
			
			
			CPCRealTimeWatchVo yj = watch.getDataFromYJ(date, hour);
			if(null == yj){
				spu.emailWarnHL("CPC实时数据：获取燕杰的"+hour+"点小时数据失败！"); 
				System.exit(0);
			}
			
			CPCRealTimeWatchVo rt = watch.getDataFromRealTime(date, hour);
			if(null == rt){
				spu.emailWarnHL("CPC实时数据：获取"+hour+"点小时数据失败！");
				System.exit(0);
			}
			
			watch.compareData(yj, rt);
			System.out.println(date + " : " + hour + " check is done!");
		} catch (Exception e) {
			e.printStackTrace();
			spu.emailWarnHL("CPC实时数据 : 小时数据检查出错！");
		}finally{
			System.exit(0);
		}
		
	}
	
}
